package com.bird.jdk.内部类;

/**
 * @author sijunqiang
 * @data 2021/4/2 21:58
 * @description: 内部类 连接到外围类
 */
public class Sequence {

    private Object[] items;

    // define
    private int next = 0;

    // construction param
    public Sequence(int size) {
        items = new Object[size];
    }

    // add element to items array
    public void add(Object x) {
        if (next < items.length) {
            items[next++] = x;
        }
    }

    // define inner classes
    private   class SequenceSelector implements Selector {

        private int i = 0;

        // 判断数组是空
        @Override
        public boolean end() {
            return i == items.length;
        }

        // 获取当前的元素
        @Override
        public Object current() {
            return items[i];
        }

        // 下一个元素
        @Override
        public void next() {
            if (i < items.length) {
                i++;
            }
        }
    }
    public Selector selector() {
        return new SequenceSelector();
    }
    public static void main(String[] args) {
        //创建对象 并指定容器
        Sequence sequence=new Sequence(10);

        //往容器中添加元素
        for (int i = 0; i < 10; i++) {
            sequence.add(i);
        }
        Selector selector = sequence.selector();
          while (!selector.end()){
              System.out.println(selector.current()+" ");
          selector.next();
          }
    }




}

class test2{
    public static void main(String[] args) {

        //创建对象 并指定容器
        Sequence sequence=new Sequence(10);

        //往容器中添加元素
        for (int i = 0; i < 10; i++) {
            sequence.add(i);
        }
        Selector selector = sequence.selector();
        while (!selector.end()){
            System.out.println(selector.current()+" ");
            selector.next();
        }
    }

}
